package love.yuanmengrobot.als.system.servlet;

import love.yuanmengrobot.als.system.dao.impl.LoginDaoImpl;
import love.yuanmengrobot.als.system.javaBean.AlsUser;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("GBK");
        response.setContentType("text/html;charset=GBK");
        String loginname = request.getParameter("username");
        String pwd = request.getParameter("password");
        System.out.println(loginname + pwd);

        request.setAttribute("loginname", loginname);

        LoginDaoImpl loginDaoImpl = new LoginDaoImpl();
        AlsUser alsUser = null;
        alsUser = loginDaoImpl.selectUser(loginname, pwd);

        if (alsUser != null) {//登录成功！

//            request.setAttribute("AlsUser", alsUser);
//            String path="../../business/academicWarning/academicWarning.jsp";
//            request.setAttribute("path", path);
//            request.getRequestDispatcher("WEB-INF/view/system/main/main.jsp").forward(request, response);
            request.getSession().setAttribute("alsUser", alsUser);
            response.sendRedirect("mainPage");

            System.out.println("登录成功");
        } else {//登录失败！
            System.out.println("登录失败");
            request.getRequestDispatcher("WEB-INF/view/system/login/loginError.jsp").forward(request, response);

        }


//        PrintStream out=new PrintStream(response.getOutputStream());
//        try {
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>com.yuanmeng.xueqingfenxi.Servlet First</title>");
//            out.println("</head>");
//            out.println("<body>");
//
//
////            out.println(name);
//            out.println("<h1>Hello World!777777777</h1>");
//
//            out.println("</body>");
//            out.println("</html>");
//            out.close();
//
//
//
//        }finally {
//            out.close();
//        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}
